package com.ruoyi.web.controller.chat;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.websocket.WebSocketUsers;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.Session;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private ISysUserService userService;

    // 当前在线人数
    @GetMapping("/onlineUserNum")
    public int onlineUserNum() {
        return WebSocketUsers.getUsers().size();
    }

    // 当前在线人员
    @GetMapping("/onlineUser")
    public List<SysUser> onlineUser() {
        Map<Long, Session> users = WebSocketUsers.getUsers();
        // 去除当前登陆本人
        SysUser user = SecurityUtils.getLoginUser().getUser();
        users.remove(user.getUserId());

        List<SysUser> list = new ArrayList<>();

        users.forEach((k, v) -> {
            SysUser sysUser = userService.selectUserById(k);
            list.add(sysUser);
        });

        return list;
    }
}
